package com.netease.xone.notification;

import preferMgr.XonePrefHelper;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import com.netease.pkgRelated.XoneConsts;
import com.netease.xone.R;
import com.netease.xone.activity.ActivityMain;
import com.netease.xone.activity.ActivityUpdateApp;


public class NotificationBarMgr {
    public static final int NOTIFICATION_NEW_VERSION = 1; 
    public static final int NOTIFICATION_NEW_ADVICE =  2; 
    public static final int NOTIFICATION_NEW_PRIVATEMSG = 3; 
    public static final int NOTIFICATION_NEW_ATME = 4; 
    public static final int NOTIFICATION_NEW_COMMENT = 5; 
    public static final int NOTIFICATION_NEW_PRAISE = 6; 
    public static final int NOTIFICATION_NEW_DYNAMIC = 7; 
    public static final int NOTIFICATION_NEW_NEWFANS = 8; 
    private static NotificationBarMgr sInstance;
    public static NotificationBarMgr getInstance(Context context){
        if(sInstance == null){
            sInstance = new NotificationBarMgr(context);
        }
        
        return sInstance;
    }
    
    private Context mContext;
    private NotificationManager mNotificationManager;
    private NotificationBarMgr(Context context){
        mContext = context;
        mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    }

     @SuppressWarnings("deprecation")
    public  void showUpdateNotification( int drawable, long when, int showContent, Bundle extras) {
            
            Notification notification = new Notification(R.drawable.icon_status_bar, null, when);
//          notification.defaults |= Notification.DEFAULT_SOUND;

            notification.flags |= Notification.FLAG_AUTO_CANCEL;
            notification.tickerText = mContext.getResources().getString(showContent);
            Intent sendIntent = new Intent();
            
            sendIntent.setClass(mContext, ActivityUpdateApp.class);
            sendIntent.putExtras(extras);
            sendIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK );
            
            PendingIntent contentIntent = PendingIntent.getActivity(mContext, 0, sendIntent, 0);
            notification.setLatestEventInfo(mContext,
                    mContext.getText(R.string.app_name),
                    mContext.getText(showContent),
                         contentIntent);
            mNotificationManager.notify(NOTIFICATION_NEW_VERSION, notification);
        }
     @SuppressWarnings("deprecation")
    public  void showPushMessage(int type, int count) {
         int contentResId = 0;
         int nId = 0;
         switch(type){
             case XoneConsts.PUSHMSG_TYPE.Advice:
                 contentResId = R.string.push_new_advice;
                 nId = NOTIFICATION_NEW_ADVICE;
                 break;
             case XoneConsts.PUSHMSG_TYPE.PrivateMsg:
                 contentResId = R.string.push_new_privatemsg;
                 nId = NOTIFICATION_NEW_PRIVATEMSG;
                 break;
             case XoneConsts.PUSHMSG_TYPE.AtMe:
                 contentResId = R.string.push_new_atme;
                 nId = NOTIFICATION_NEW_ATME;
                 break;
             case XoneConsts.PUSHMSG_TYPE.Comment:
                 contentResId = R.string.push_new_comment;
                 nId = NOTIFICATION_NEW_COMMENT;
                 break;
             case XoneConsts.PUSHMSG_TYPE.Praise:
                 contentResId = R.string.push_new_praise;
                 nId = NOTIFICATION_NEW_PRAISE;
                 break;
             case XoneConsts.PUSHMSG_TYPE.Dynamic:
                 contentResId = R.string.push_new_dynamic;
                 nId = NOTIFICATION_NEW_DYNAMIC;
                 break;
             case XoneConsts.PUSHMSG_TYPE.NewFans:
                 contentResId = R.string.push_new_newfans;
                 nId = NOTIFICATION_NEW_NEWFANS;
                 break;
         }
         String content = null;
         if(contentResId >0){
             content = mContext.getResources().getString(contentResId,count);
         }
         
         Intent sendIntent = new Intent();
         
         sendIntent.setClass(mContext, ActivityMain.class);
         sendIntent.putExtra(XoneConsts.BUNDLE_KEY.PUSH_TYPE,type);
         sendIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK );
         
         PendingIntent contentIntent = PendingIntent.getActivity(mContext, 0, sendIntent, PendingIntent.FLAG_UPDATE_CURRENT);
         
        @SuppressWarnings("deprecation")
        Notification notification = new Notification(R.drawable.icon_status_bar, null, System.currentTimeMillis());
         if(XonePrefHelper.getSoundOn(mContext)){
             notification.defaults |= Notification.DEFAULT_SOUND;
         }
         if(XonePrefHelper.getShockOn(mContext)){
             notification.defaults |= Notification.DEFAULT_VIBRATE;
         }
         notification.flags |= Notification.FLAG_AUTO_CANCEL;
         notification.tickerText = "网易看游戏新消息来了";
         
        notification.setLatestEventInfo(mContext, mContext.getText(R.string.app_name), content,
                contentIntent);
        mNotificationManager.notify(nId, notification);
     }
       
    public void cancelUpdateNotify() {
        mNotificationManager.cancel(NOTIFICATION_NEW_VERSION);
    }
    public void cancleAll(){
        mNotificationManager.cancelAll();
    }
    public void cancelPushAll(){
        mNotificationManager.cancel(NOTIFICATION_NEW_ADVICE);
        mNotificationManager.cancel(NOTIFICATION_NEW_PRIVATEMSG);
        mNotificationManager.cancel(NOTIFICATION_NEW_ATME);
        mNotificationManager.cancel(NOTIFICATION_NEW_COMMENT);
        mNotificationManager.cancel(NOTIFICATION_NEW_PRAISE);
        mNotificationManager.cancel(NOTIFICATION_NEW_DYNAMIC);
        mNotificationManager.cancel(NOTIFICATION_NEW_NEWFANS);
    }
}
